package net.vkits.anjiamiao.application;

import net.vkits.anjiamiao.domain.model.Feedback;
import net.vkits.anjiamiao.domain.repository.FeedbackRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * Created by ethan-liu on 2017/1/5.
 */
@Service
public class FeedbackApplicationService {
    private static final Logger logger = LoggerFactory.getLogger(FeedbackApplicationService.class);

    @Resource
    private FeedbackRepository feedbackRepository;

    public void submitFeedback(String content, String cellphone) {
        Optional<Feedback> feedbackOptional = feedbackRepository.findByCellphone(cellphone);
        feedbackOptional.orElse(feedbackRepository.save(new Feedback(content, cellphone)));
    }

    public Feedback findById(String id) {
        return feedbackRepository.findOne(Long.parseLong(id));
    }

    public Page<Feedback> getFeedbacks(Pageable pageable) {
        return feedbackRepository.findAll(pageable);
    }
}
